<?

/*******************************************************************************************************
/*	MY_Controller extension for CodeIgniter
/*	Author: Jesse Terry (jessedterry@gmail.com)
/*	This library performs the following:
/*
/*	- Automatically loads all models
/*	- Provides interface to check for admin / user login per each page that extends Iterator
/*	- Function uri_assoc($var) enables a uri assoc var to be checked for an return NULL if not found
/*	- Prepares validation vars to populate a form entry based on the results of a database result set
/*
/*	Admin controllers will work on the basic CI controller/method URI logic
/*	parent::MY_Controller(TRUE) will assign $this->uri_assoc beginning with the 3rd segment
/*
/*	Non-admin controllers will work on InkType's view/vars URI logic
/*  parent::MY_Controller() will assign $this->uri_assoc beginning with the 2nd segment
/*
/*
/******************************************************************************************************/

class MY_Controller extends Controller {

	var $uri_assoc;

	var $page = 1;

	var $is_admin;

	function MY_Controller($is_admin = FALSE, $is_user = FALSE) {

		parent::Controller();

		$this->uri_assoc = $this->uri->uri_to_assoc(3);

		$this->_set_page();

		$this->_autoload_models();

		$this->_check_auth($is_admin, $is_user);
		
		$this->load->library('inktype');

		$this->css_dir = base_url() . 'system/application/views/';
		
		$this->mdl_visitors->save();

	}

	function _autoload_models() {

		foreach (directory_map('./system/application/models/', TRUE) AS $file) {

			if (substr($file, 0, 4) == 'mdl_') {

				$model = substr($file, 0, -4);

				$this->load->model($model);

			}

		}

	}

	function _check_auth($is_admin, $is_user) {

		if ($is_admin) {

			$this->is_admin = TRUE;

			if (!$this->mdl_user_types->is_admin($this->session->userdata('user_type_id'))) {

				redirect('sessions/login');

			}

		}

		elseif ($is_user) {

			if (!$this->session->userdata('is_user')) {

				redirect('sessions/login');

			}

		}

	}

	function uri_assoc($var, $n = 3) {

		$uri_assoc = $this->uri->uri_to_assoc($n);

		if (isset($uri_assoc[$var])) {

			return $uri_assoc[$var];

		}

		else {

			return NULL;

		}

	}

	function _load_admin_view($view, $data = NULL) {

		$this->load->view('admin/' . $view, $data);

	}

	function _set_page() {

		if ($this->uri_assoc('page')) {

			$this->page = $this->uri_assoc['page'];

		}

	}

}

?>